home *** CD-ROM | disk | FTP | other *** search
- /*
- ** GETDOC.C [edit EMAIL.H before compiling]
- **
- ** This is an automated email response program. Users
- ** send email to your POP3 account with no body and with
- ** either "LIST" or "GET document" in the subject field.
- ** This program scans your POP3 mailbox for LIST and GET
- ** commands in the subject field and responds by replying
- ** with the requested document, provided that it exists.
- ** The requested email is deleted.
- */
-
- #include <windows.h>
- #include <stdio.h>
- #include "see.h"
- #include "email.h"
-
- #define MAX_EMAIL_BODY_SIZE 1500
-
- #define BUFF_SIZE 2048
- #define TEMP_SIZE 128
- #define MAX_LIST 20
- #define MAX_STR 65
-
- static char Buffer[BUFF_SIZE];
- static char Temp[TEMP_SIZE];
- static char Reply[TEMP_SIZE];
- static struct
- {short MsgNbr;
- char Address[1+MAX_STR];
- char Document[1+MAX_STR];
- } EmailData[MAX_LIST];
- static int EmailCount = 0;
-
- void ErrorExit(int Code)
- {seeErrorText(Code,(LPSTR)Buffer,512);
- printf("SEE Error %d: %s\n", Code, Buffer);
- seeClose();
- exit(1);
- }
-
- #define TAB 0x09
-
- LPSTR FirstNonBlank(LPSTR Ptr)
- {char c;
- while(1)
- {c = *Ptr;
- /* skip white space */
- if((c!=' ')&&(c!=TAB)) return Ptr;
- Ptr++;
- }
- }
-
- void StripCRLF(LPSTR Ptr)
- {char c;
- while(1)
- {c = *Ptr;
- if(c=='\0') return;
- if((c=='\r')||(c=='\n'))
- {*Ptr = '\0';
- return;
- }
- Ptr++;
- }
- }
-
- int IsLeft(LPSTR Ptr1, LPSTR Ptr2)
- {char c1, c2;
- while(1)
- {c1 = *Ptr1++;
- c2 = *Ptr2++;
- if(c2=='\0') return TRUE;
- if((c1>='a')&&(c1<='z')) c1 = c1 - 'a' + 'A';
- if((c2>='a')&&(c2<='z')) c2 = c2 - 'a' + 'A';
- if(c1!=c2) return FALSE;
- }
- }
-
- void StrCpy(LPSTR Dst, LPSTR Src)
- {char c;
- while(1)
- {c = *Src++;
- if((c=='\r')||(c=='\n')) c = '\0';
- *Dst++ = c;
- if(c=='\0') break;
- }
- }
-
- void main(void)
- {int i, n;
- int Code;
- int BufSize;
- int NbrMsg;
- int Version;
- long MsgSize;
- LPSTR Ptr;
- FILE *FilePtr;
- Version = seeStatistics(SEE_GET_VERSION);
- printf("SEE4C Version %1x.%1x.%1x\n",0x0f&(Version>>8),0x0f&(Version>>4),0x0f&Version);
- /* define diagnostics log file */
- seeStringParam(SEE_LOG_FILE, (LPSTR)"getdoc.log");
- /* connect to POP3 server */
- printf("Connecting to POP3 server %s\n",POP3_HOST_NAME);
- Code = seePop3Connect(
- (LPSTR)POP3_HOST_NAME,
- (LPSTR)POP3_USER_NAME,
- (LPSTR)POP3_PASSWORD);
- if(Code<0) ErrorExit(Code);
- /* get # messages waiting */
- puts("Getting message count...");
- NbrMsg = seeGetEmailCount();
- if(NbrMsg<0) ErrorExit(NbrMsg);
- printf("%d messages waiting.\n", NbrMsg);
- /* read message headers */
- for(i=1;i<=NbrMsg;i++)
- {if(EmailCount==MAX_LIST)
- {printf("EmailData overflow\n");
- break;
- }
- /* get size of waiting email (counting header lines) */
- MsgSize = seeGetEmailSize(i);
- /* Message size should be "small" since it should have no body */
- if(MsgSize<=MAX_EMAIL_BODY_SIZE)
- {/* read message i */
- printf("Reading email message %d \n",i);
- /* read header + first 3 lines */
- BufSize = seeGetEmailLines(i, 3, (LPSTR)Buffer, BUFF_SIZE);
- if(BufSize<0) ErrorExit(Code);
- Buffer[BufSize] = '\0';
- /* look for "FROM: " line */
- n = seeExtractText((LPSTR)Buffer, "From: ", (LPSTR)Reply, TEMP_SIZE);
- if(n>0)
- {Ptr = (LPSTR)Reply+6;
- StripCRLF(Ptr);
- ///printf("%s\n", (LPSTR)Reply);
- if(strchr(Ptr,'<')==NULL)
- {/* sourround with angle brackets */
- EmailData[EmailCount].Address[0] = '<';
- strcpy(&EmailData[EmailCount].Address[1],Ptr);
- strcat(&EmailData[EmailCount].Address[2],">");
- }
- else strcpy(&EmailData[EmailCount].Address[0],Ptr);
- /* look for "SUBJECT: " line */
- n = seeExtractText((LPSTR)Buffer, "Subject: ", (LPSTR)Temp, TEMP_SIZE);
- if(n>0)
- {///printf("%s", (LPSTR)Temp);
- Ptr = FirstNonBlank((LPSTR)&Temp[8]);
- if(IsLeft((LPSTR)Ptr,(LPSTR)"LIST"))
- {printf("Adding LIST request.\n");
- EmailData[EmailCount].MsgNbr = i;
- StrCpy((LPSTR)EmailData[EmailCount].Document,(LPSTR)"list.txt");
- EmailCount++;
- }
- else if(IsLeft((LPSTR)Ptr,(LPSTR)"GET "))
- {printf("Adding GET request.\n");
- Ptr = FirstNonBlank((LPSTR)(Ptr+3));
- EmailData[EmailCount].MsgNbr = i;
- StrCpy((LPSTR)EmailData[EmailCount].Document,Ptr);
- EmailCount++;
- }
- else printf("Cannot recognize subject command [%s]\n", Ptr);
- }
- else printf("Missing Subject:\n");
- }
- else printf("Missing From:\n");
- }
- else printf("Email body is too large.\n");
- } /* end-for */
-
- printf("%d documents requested\n", EmailCount);
- /* display requests */
- for(i=0;i<EmailCount;i++)
- {printf("%2i: Msg=%d From=[%s] Doc=[%s]\n",
- i+1, EmailData[i].MsgNbr, EmailData[i].Address, EmailData[i].Document);
- // Suggestion: log date & time stamped requests to disk.
- }
-
- /* delete email message requests in REVERSE order */
- for(i=EmailCount-1;i>=0;i--)
- {n = EmailData[i].MsgNbr;
- printf("Deleting message # %d\n",n);
- Code = seeDeleteEmail(n);
- if(Code<0) ErrorExit(Code);
- }
- /* close connection to POP3 server */
- seeClose();
-
- /* anything to respond to ? */
- if(EmailCount==0) exit(0);
-
- /* connect to SMTP server */
- printf("Connecting to SMTP server %s\n",SMTP_HOST_NAME);
- Code = seeSmtpConnect(
- (LPSTR)SMTP_HOST_NAME,
- (LPSTR)YOUR_EMAIL_ADDR,
- (LPSTR)NULL);
- if(Code<0) ErrorExit(Code);
-
- /* send email response*/
- for(i=0;i<EmailCount;i++)
- {printf("Responding to message # %d at %s\n",
- EmailData[i].MsgNbr, (LPSTR)EmailData[i].Address);
- FilePtr = fopen(EmailData[i].Document,"rb");
- if(FilePtr)
- {fclose(FilePtr);
- Code = seeSendEmail(
- (LPSTR)EmailData[i].Address,
- (LPSTR)NULL,
- (LPSTR)NULL,
- (LPSTR)"Automated Response",
- (LPSTR)"Your requested document is attached.",
- (LPSTR)EmailData[i].Document);
- if(Code<0) ErrorExit(Code);
- }
- else
- {printf("Cannot find document %s\n",EmailData[i].Document);
- Code = seeSendEmail(
- (LPSTR)EmailData[i].Address,
- (LPSTR)NULL,
- (LPSTR)NULL,
- (LPSTR)"Automated Response",
- (LPSTR)"Requested document not found.",
- (LPSTR)NULL);
- if(Code<0) ErrorExit(Code);
- }
- }
- seeClose();
- printf("Done.\n");
- } /* end main */
-
-
-